package com.tom.architect.day05;

import androidx.annotation.CallSuper;

/*
import com.butterknife.annotations.BindView;
import com.google.auto.service.AutoService;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.TypeName;
import com.squareup.javapoet.TypeSpec;
import com.sun.source.tree.ClassTree;
import com.sun.source.util.Trees;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.TreeScanner;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Filer;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.Processor;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.MirroredTypeException;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;
*/


/**
 * Added by Tom on 2023/12/08.
 * 没改动代码的情况下， 是不会生成代码的。
 */
// com.butterknife.compiler module中的类。
/*
@AutoService(Processor.class) // 必须添加。
public class ButterKnifeProcessor extends AbstractProcessor {
    private Filer mFiler;
    private Elements mElementUtils;
//    private Trees trees;
//    private Types mTypeUtils;
//    private final Map<QualifiedId, Id> symbols = new LinkedHashMap<>();

    @Override
    public synchronized void init(ProcessingEnvironment processingEnvironment) {
        super.init(processingEnvironment);
        mFiler = processingEnvironment.getFiler();
        mElementUtils = processingEnvironment.getElementUtils();
//        mTypeUtils = processingEnvironment.getTypeUtils();
//
//        try {
//            trees = Trees.instance(processingEnv);
//        } catch (IllegalArgumentException ignored) {
//        }
    }

    // 1.指定处理的版本；
    @Override
    public SourceVersion getSupportedSourceVersion() {
        return SourceVersion.latestSupported();
    }

    */
/**
     * 2.用来指定支持的 AnnotationTypes
     *//*

    @Override
    public Set<String> getSupportedAnnotationTypes() {
        Set<String> types = new LinkedHashSet<>();
        for (Class<? extends Annotation> annotation : getSupportedAnnotations()) {
            types.add(annotation.getCanonicalName());
        }
        return types;
    }

    */
/**
     * 参考了 ButterKnife 的写法
     *
     * @return
     *//*

    private Set<Class<? extends Annotation>> getSupportedAnnotations() {
        Set<Class<? extends Annotation>> annotations = new LinkedHashSet<>();
        // 需要解析的自定义注解
        annotations.add(BindView.class);
        // TODO OnClick
        return annotations;
    }


    // process方法代表的是，有注解就会进来。但是这里面是一团乱码。
    @Override
    public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) {
        // System.out.println("------------------");
        Set<? extends Element> elements = roundEnvironment.getElementsAnnotationWith(BindView.class);
        // 解析属性，activity-> list<Element>
//        for (Element element: elements) {
//            // view：element.getSimpleName().toString();
//            // 拿到activity: enclosingElement.getSimpleName().toString();
//            Element enclosingElement =element.getEnclosingElement();
//        }
        // 1.解析，确保一个Activity对应一个list。
        Map<Element, List<Element>> elementMap = new LinkedHashMap<>();
        for (Element element : elements) {
            Element enclosingElement = element.getEnclosingElement();
            List<Element> viewBindElements = elementMap.get(enclosingElement);
            if (viewBindElements == null) {
                viewBindElements = new ArrayList();
                elementMap.put(enclosingElement, viewBindElements);
            }
            viewBindElements.add(element); // 添加。
        }

        // 2.循环生成代码。
        for (Map.Entry<Element, List<Element>> entry : elementMap.entrySet()) {
            Element enclosingElement = entry.getKey();
            List<Element> viewBindElements = entry.getValue();

            String activityClsNameStr = enclosingElement.getSimpleName().toString();
            ClassName activityClassName = ClassName.bestGuess(activityClsNameStr);
            // 传递包名+ 文件名
            ClassName unbinderClassName = ClassName.get("com.butterknife", "Unbinder");
            // public final class xxxActivity_ViewBinding implements Unbinder
            TypeSpec.Builder classBuilder = TypeSpec.classBuilder(activityClsNameStr + "_ViewBinding")
                    .addModifiers(Modifier.FINAL, Modifier.PUBLIC)
                    .addSuperinterface(unbinderClassName)
                    .addField(activityClassName, "target", Modifier.PRIVATE)  // 添加属性。
                    ;
            // 实现unbind方法。
            MethodSpec.Builder unbindMethodBuilder = MethodSpec.methodBuilder("unbind")
                    .addAnnotation(Override.class)
                    .addAnnotation(CallSuper.class)
                    .addModifiers(Modifier.FINAL, Modifier.PUBLIC);
            */
/* MainActivity target = this.target;
            if (target == null) throw new IllegalStateException("Bindings already cleared.");
            this.target = null;*//*

            unbindMethodBuilder.addStatement("$T target = this.target", activityClassName);
            unbindMethodBuilder.addStatement("if (target == null) throw new IllegalStateException(\"Bindings already cleared.\")");
            unbindMethodBuilder.addStatement("this.target = null");



            // 构造函数，
            MethodSpec.Builder constructorMethodBuilder = MethodSpec.methodBuilder()
                 .addParameter(activityClassName, "target");
            constructorMethodBuilder.addStatement("this.target = target");
            // findViewById属性：添加 target.textView1 = Utils.findViewById(target,R.id.tv1);
            for (Element bindViewElement : elements) {
                String filedName = bindViewElement.getSimpleName().toString(); // 名字
                ClassName utilClassName = ClassName.get("com.butterknife", "Utils"); // Utils类名称
                int resId = bindViewElement.getAnnotation(BindView.class).value();
                QualifiedId qualifiedId = elementToQualifiedId(bindViewElement, resId);
                Id id = getId(qualifiedId);
                CodeBlock codeBlock = id.code; // id
                */
/*CodeBlock codeBlock = CodeBlock.of("$L", resId);
                System.out.println("--------------------------->" + codeBlock.toString());*//*

                constructorBuilder.addStatement("target.$L = $T.findViewById(target,$L)",
                        filedName, utilClassName, codeBlock);

                // unbind方法中，添加释放代码。
                unbindMethodBuilder.addStatement("target.$L = null", filedName);
            }

            classBuilder.addMethod(unbindMethodBuilder.build()); // 添加方法。
            classBuilder.addMethod(constructorMethodBuilder.build()); // 添加构造方法。


            // 生成类。先看效果。
            try {
                String packageName = mElementUtils.getPackgeOf(enclosingElement)
                        .getQualifiedName().toString();
                JavaFile.builder(packageName, classBuilder.build())
                        .addFileComment("butterKnife 自动生成注释")
                        .build().writeTo(mFiler);
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }



    private Id getId(QualifiedId qualifiedId) {
        if (symbols.get(qualifiedId) == null) {
            symbols.put(qualifiedId, new Id(qualifiedId.id));
        }
        return symbols.get(qualifiedId);
    }

}
*/
